package com.cskaoyan.market.config;

import com.cskaoyan.market.filter.CORSFilter;
import com.cskaoyan.market.filter.CharFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import java.util.Arrays;

@Configuration
public class WebConfig {

    @Bean
    public FilterRegistrationBean<OncePerRequestFilter> crosFilterRegistrationBean(){
        FilterRegistrationBean<OncePerRequestFilter> bean = new FilterRegistrationBean<>();
        bean.setFilter(new CORSFilter());
        //我们设定的filter跨域只针对后台管理系统生效，小程序不需要
        bean.setUrlPatterns(Arrays.asList("/admin/*"));
        bean.setOrder(1);
        return bean;
    }

    @Bean
    public FilterRegistrationBean<OncePerRequestFilter> charFilterRegistrationBean(){
        FilterRegistrationBean<OncePerRequestFilter> bean = new FilterRegistrationBean<>();
        bean.setFilter(new CharFilter());
        bean.setUrlPatterns(Arrays.asList("/*"));
        bean.setOrder(2);
        return bean;
    }

    @Bean
    public MultipartResolver multipartResolver(){
        return new CommonsMultipartResolver();
    }
}